import { _decorator, Component, Node, Collider, ITriggerEvent } from 'cc';
import { Contant } from '../framework/Contant';
const { ccclass, property } = _decorator;

const OUTFRANGE = -500;

@ccclass('bullet')
export class Bullet extends Component {

    @property
    public bulletSpeed = 3
 

    start() {

    }

    onEnable() {
        const collider = this.getComponent(Collider)
        collider.on("onTriggerEnter",this._onTriggerEnter, this)
    }

    update(deltaTime: number) {
        const postion = this.node.getPosition()
        const newZ = this.node.getPosition().z - this.bulletSpeed
        this.node.setPosition(postion.x, postion.y, newZ);

        if(newZ < OUTFRANGE){
            this.node.destroy()
        }
    }

    _onTriggerEnter(event: ITriggerEvent) {

        const otherGroup = event.otherCollider.getGroup()
        if(otherGroup === Contant.CollisionType.ENEMY_BULLET || otherGroup === Contant.CollisionType.ENEMY_PLANE){
            this.node.destroy();
        }

    }
}


